<template>
  <div>
    <!-- 箱型统计 2018/8/15 Jelly -->
    <Row style="padding-bottom: 10px;">
      <i-col :md="24">
        <i-col :span="16">
          <div style="font-size: 18px;font-weight: 600;line-height: 36px;">{{ this.tabParam.stationName }} 箱型统计</div>
        </i-col>
        <i-col :span="8">
          <Button type="primary" size="large" @click="exportContainerTypeStatics" style="float: right">
            <Icon type="ios-download-outline"></Icon>
            导出数据
          </Button>
        </i-col>
      </i-col>
    </Row>
    <Row>
      <i-col>
        <tablePage ref="containerTypeStaticsTab" :staticData="tabData" :isStartLoad="false" :mode="1">
          <template slot-scope="props">
            <Table :columns="containerColumn" border :data="props.data" @on-sort-change="props.sortChange"></Table>
          </template>
        </tablePage>
      </i-col>
    </Row>
  </div>
</template>

<script>
  import tablePage from '@components/table/tablePage.vue'
  import { getStationContainerTypeStatics } from '@api/containerPlatform/containerManagement'
  import _ from 'lodash'
  import { exportUtil } from '@/vendor/exportInvoke'

  export default {
    name: 'containerTypeStatics',
    data () {
      let tabParam = {
        stationName: ''
      }
      let tabData = []
      let containerColumn = [
        {
          title: '箱型',
          width: 150,
          key: 'containerType'
        },
        {
          title: '站内重箱',
          key: 'inHeavy',
          width: 150
        },
        {
          title: '站内空箱',
          key: 'inEmpty',
          minWidth: 250
        },
        {
          title: '在途',
          key: 'outWay',
          width: 120
        },
        {
          title: '站外待回',
          key: 'outBack',
          width: 120
        },
        {
          title: '合计',
          key: 'total',
          width: 100
        }
      ]
      let datas = {
        tabParam,
        containerColumn,
        tabData,
        getStationContainerTypeStatics
      }
      return datas
    },
    props: {
      stationQuantityWarningInfo: {}
    },
    methods: {
      getStationContainerTypeStaticsData () {
        this.getStaticsData()
      },
      exportContainerTypeStatics () {
        let result = this.fasetJson(this.tabData)
        exportUtil.export2Excel(this.tabParam.stationName + '-箱型统计', result.columns, result.data)
      },
      getStaticsData () {
        getStationContainerTypeStatics(this.tabParam).then(res => {
          if (res.data.isSuccess === 1) {
            let rstData = res.data.data
            let powerMappingGroupBy = _.groupBy(rstData, 'containerType')
            let resData = []
            let lastRow = { containerType: '总计', inEmpty: 0, inHeavy: 0, outWay: 0, outBack: 0, total: 0 }
            _.forIn(powerMappingGroupBy, (value, key) => {
              let inEmpty = 0
              let inHeavy = 0
              let outWay = 0
              let outBack = 0
              let total = 0
              _.forIn(value, (v, k) => {
                total += parseInt(v.tCount)
                if ((v.curStateId >= 10 && v.curStateId < 30) || (v.curStateId >= 32 && v.curStateId < 53)) {
                  if (v.isEmpty === 1) {
                    inHeavy += parseInt(v.tCount)
                  } else {
                    inEmpty += parseInt(v.tCount)
                  }
                } else if (v.curStateId >= 30 && v.curStateId <= 31) {
                  outWay += parseInt(v.tCount)
                } else if (v.curStateId >= 53 && v.curStateId < 80) {
                  outBack += parseInt(v.tCount)
                }
              })
              lastRow.inEmpty += inEmpty
              lastRow.inHeavy += inHeavy
              lastRow.outWay += outWay
              lastRow.outBack += outBack
              lastRow.total += total
              resData.push({ containerType: key, inEmpty: inEmpty, inHeavy: inHeavy, outWay: outWay, outBack: outBack, total: total })
            })
            resData = _.sortBy(resData, function (o) {
              return o.containerType
            })
            resData.push(lastRow)
            this.tabData = resData
          }
        })
      },
      fasetJson (tabData) {
        let result = {
          columns: [
            {
              title: '箱型',
              key: 'containerType'
            },
            {
              title: '站内重箱',
              key: 'inHeavy'
            },
            {
              title: '站内空箱',
              key: 'inEmpty'
            },
            {
              title: '在途',
              key: 'outWay'
            },
            {
              title: '站外待回',
              key: 'outBack'
            },
            {
              title: '合计',
              key: 'total'
            }
          ],
          data: []
        }
        tabData.forEach((item) => {
          let dataItem = {
            containerType: item.containerType,
            inHeavy: item.inHeavy,
            inEmpty: item.inEmpty,
            outWay: item.outWay,
            outBack: item.outBack,
            total: item.total
          }
          result.data.push(dataItem)
        })
        return result
      }
    },
    watch: {},
    mounted () {
      this.tabParam.stationName = this.stationQuantityWarningInfo.stationName
      console.log(this.tabParam)
      this.getStationContainerTypeStaticsData()
    },
    computed: {},
    components: { tablePage }
  }
</script>
<style scoped>
</style>
